<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2018/10/22
 * Time: 11:10
 */
use app\assets\AppAsset;
use app\common\CommonHelper;

AppAsset::register($this);

$base = Yii::$app->request->baseUrl;
AppAsset::addCss($this,$base . '/app/css/skin/jedate.css');
AppAsset::addScript($this,$base . '/app/js/jedate.js');
AppAsset::addScript($this,$base . '/app/js/app-js.js');
AppAsset::addCss($this,$base . '/app/css/vest-list.css');

$model = new CommonHelper();

$tomorrowDate = $model->getTomorrowDateHI();
$yesterdayDate = $model->getYesterdayDateHI();

?>

<style>

</style>
<div class="search-box">
    <p class="datep" style="margin-left: 1.67rem;">
        <img src="<?=$base?>/app/images/new/rc_icon_date.png" class="date-icon">
        <input class="datainp" id="datebut" type="text" placeholder="请选择"  readonly>

    </p>

    <span class="date-split">至</span>

    <p class="datep">

        <img src="<?=$base?>/app/images/new/rc_icon_date.png" class="date-icon">
        <input class="datainp" id="datebut2" type="text" placeholder="请选择"  readonly>
    </p>

    <select id="cityID" style="margin-left:1.67rem;float: left" onchange="searchFunction()">
        <option value="-1">所在城市</option>
        <option value="179">杭州市</option>
        <?php for($index=0;$index<count($cities);$index++){?>
            <option value="<?= $cities[$index]['city_code']?>"><?= $cities[$index]['city_name']?></option>
        <?php }?>
    </select>

    <div style="clear: both"></div>
</div>

<div class="search-box" style="margin-top: 0;margin-bottom: 1.67rem">

    <span style="float: left;margin-left:1.67rem;font-size: 16px;line-height: 3.3rem">含商家:</span>

    <select id="dynamicBar" style="float: left" onchange="searchFunction()">
        <option value="-1">所有</option>
        <option value="0">是</option>
        <option value="1">否</option>
    </select>

    <span style="float: left;margin-left:1.67rem;font-size: 16px;line-height: 3.3rem">发布者:</span>

    <select id="dynamicUser" style="float: left" onchange="searchFunction()">
        <option value="-1">全部</option>
        <option value="0">真实用户</option>
        <option value="1">马甲</option>
    </select>

    <span style="float: left;margin-left:1.67rem;font-size: 16px;line-height: 3.3rem">排序规则:</span>

    <select id="sortType" style="float: left" onchange="searchFunction()">
        <option value="-1">默认</option>
        <option value="0">最新回复时间倒序</option>
    </select>

    <div class="clear"></div>
</div>

<div class="search-box" style="margin-top: 0;margin-bottom: 1.67rem">
    <div class="datep" style="margin-left: 1.67rem;"><input id="nameId" class="name-input" placeholder="请输入"></div>

    <img id="search-img" onclick="searchFunction()" class="search-icon" style="margin-left: 1.67rem;" src="<?=$base?>/app/images/new/rc_btn_search_n.png">

    <div class="button-div" style="margin-left: 3.6rem" onclick="goToAddDynamic()">新增</div>

    <div style="clear:both;"></div>
</div>

<div id="adjustment" class="dialog-topic" hidden="hidden">
    <div class="topic-box">
        <p>调整点赞、浏览</p>

        <table style="font-size: 16px; color: black; margin-left: 6.67rem; margin-bottom: 1.67rem;">
            <tr>
                <td>新点赞量：</td>
                <th><input type="number" id="likeCount" placeholder="请输入需要新增点赞量" style="margin: 1rem;"></th>
            </tr>

            <tr>
                <td>新浏览量：</td>
                <th><input type="number" id="browseCount" placeholder="请输入需要新增浏览量" style="margin: 1rem;"></th>
            </tr>
        </table>

        <div class="footer-div">
            <div class="button-div" id="doAdjustment">确定</div>
            <div class="button-div" style="margin-left: 1.5rem" onclick="hideAdjustment()">取消</div>
        </div>
    </div>
</div>

<div class="table-box" style="background: none; min-width:1010px;">
    <table id="table" style="background: #FFFFFF;"></table>

    <div class="showContent" id="show_content" style="position: absolute;display: none;">
        <span class="content" id="content_id">test</span>
    </div>
</div>

<script>

    //初始化事件控件的日期
    var tomorrowDate = '<?php echo $tomorrowDate?>';
    var yesterdayDate = '<?php echo $yesterdayDate?>';
    $("#datebut").val("2018-09-01 00:00");
    $("#datebut2").val(tomorrowDate);
    //初始化表格设置
    $('#table').bootstrapTable({

//        height: 525,//高度
        toolbar: '#toolbar',        //工具按钮用哪个容器
        clickToSelect: true,//点击行即可选中单选/复选框
        singleSelect: false,//复选框只能选择一条记录
        search: false,//是否显示右上角的搜索框
        striped: true,      //是否显示行间隔色
        cache: false,            //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        sidePagination: "server", //服务端处理分页
        pagination: true,     //是否显示分页（*）
        sortable: false,           //是否启用排序
        sortOrder: "asc",          //排序方式
        method: 'GET',      //请求方式（*）
        pageNumber:1,      //初始化加载第一页，默认第一页
        pageSize: 10,      //每页的记录行数（*）
//        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        cardView: false,          //是否显示详细视图
        queryParams:setParams,
        uniqueId:'dynamic_id',
        url:'dynamic-data',
        responseHandler:dataProcess,

        columns: [{
            field: 'dynamic_id',
            align: 'center',
            valign: 'middle',
            title: '动态ID'
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '动态内容',
            formatter:function(value,row,index) {

                var str = row.dynamic_content;
                var html = '';
                if(str.length > 65){
                    html = '<div id="content_text' + index + '" class="content-push-body" content="'+str +'" onmouseover="dialogShow(this)" onmouseout="dialogHide()" dynamic-id="'+row.dynamic_id+'" onclick="goToDetail(this)"><span class="content-text" style="color: #0000ff" >' + str + '</span></div>';
                }else{
                    html = '<div class="content-push-body" dynamic-id="'+row.dynamic_id+'" onclick="goToDetail(this)"><span class="content-text" style="color: #0000ff" >' + str + '</span></div>';
                }
                return html;
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '城市',
            formatter:function(value,row,index) {
                return row.city?row.city.city_name:"无";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '回复/点赞/浏览',
            formatter:function(value,row,index) {
                return row.comment_count + "/" + row.like_count + "/" + row.true_browse_count;
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '状态',
            formatter:function(value,row,index) {
                return row.is_deleted == 0?"线上":"已删除";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '权重数',
            formatter:function(value,row,index) {
                return '<input type="number" style="border:1px solid #F0F0F0;border-radius:6px;width:60px;padding: 6px 12px;" value="'+row.weight_value+'" dynamicid="'+row.dynamic_id+'" onblur="setWeight(this)" oninput="if(value.length>2)value=value.slice(0,2)">';
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '发布者',
            formatter:function(value,row,index) {
                return row.user?row.user.user_nickname:"无";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '商家',
            formatter:function(value,row,index) {
                return row.bar?row.bar.bar_name:"无";
            }
        },{
            field: '',
            align: 'center',
            valign: 'middle',
            title: '操作',
            formatter:function(value,row,index) {
                var html = "";
                if(row.is_deleted == 0){
                    html = '<div class="btn_cancel" onclick="operateController.operateFunc('+row.dynamic_id+',1)">下架</div> <div class="btn_cancel" onclick="eidtAdjustment('+row.dynamic_id+')">调整量</div>'
                }else{
                    html = '<div class="btn_cancel" onclick="operateController.operateFunc('+row.dynamic_id+',0)">上架</div> <div class="btn_cancel" onclick="eidtAdjustment('+row.dynamic_id+')">调整量</div>'
                }

                return html;
            }
        }]
    });

    function dataProcess(res){
        var data = res;
        return {
            'rows':data.rows,
            'total':data.total
        }

    }
    function setParams(params){

        var start_date = $('#datebut').val(); //按日期搜索
        var end_date = $('#datebut2').val();

        if (start_date && start_date != undefined && end_date && end_date != undefined){
            params.start_date = start_date;
            params.end_date = end_date;
        }

        var content = $("#nameId").val();
        if (content && content != undefined && content.length > 0){
            params.dynamic_title = content;
        }

        var cityCode = $("#cityID").val();
        if(cityCode != -1)
            params.city_code = cityCode;

        var dynamicBar = $("#dynamicBar").val();
        if(dynamicBar != -1)
            params.dynamic_bar = dynamicBar;

        var dynamicUser = $("#dynamicUser").val();
        if(dynamicUser != -1)
            params.dynamic_user = dynamicUser;

        var sortType = $("#sortType").val();
        if(sortType != -1)
            params.sort_type = sortType;

        return params;
    }

    var detail = $('#show_content');
    var text = $('#content_id');
    var _w=document.documentElement.clientWidth;
    var paddingLeft = (_w - 1200)/2;

    function dialogShow(content){
        var string = $(content).attr('content');
        var top =  $(content).offset().top;
        var left =  $(content).offset().left;


        text.text(string);
        var height = detail.outerHeight(true);
        var width = detail.outerWidth(true);
        detail.css("top",top - height);
        detail.css('left',left - paddingLeft - 10);
        detail.show();
    }

    function dialogHide(){
        detail.hide();
    }

    function searchFunction(){
        $('#table').bootstrapTable('refreshOptions', {pageNumber: 1});
    }

    // 获取权重值
    function setWeight(view){
       // console.log(view);
        weightController.weightFunc($(view).attr("dynamicid"),$(view).val());
    }

    // 添加权重值
    var weightController = {
        weightApi : "dynamic-weight",
        weightFunc:function(dynamic_id , weight){
            var param = {
                dynamic_id : dynamic_id,
                weight_value : weight
            };

            sendAjax(this.weightApi,"POST",param,true,function(res){
                var data = JSON.parse(res);
                if(data.code == 0){
                    console.log(data);
                    $('#table').bootstrapTable('refresh');
                }
            });
        }
    };


    var operateController = {
        operateApi : "operate-dynamic",
        operateFunc:function(dynamic_id , type){

            var confirmStr = "确认删除动态吗？";

            if(type == 0)
                confirmStr = "确认恢复动态吗？";

            if(confirm(confirmStr)){
                var param = {
                    dynamic_id:dynamic_id,
                    type:type
                };

                sendAjax(this.operateApi,"POST",param,true,function(res){
                    var data = JSON.parse(res);
                    if(data.code == 0)
                        $('#table').bootstrapTable('refresh');
                });
            }

        }
    };

    function goToDetail(view){
        window.open("dynamic-detail?dynamic_id="+$(view).attr("dynamic-id"));
    }

    function goToAddDynamic(){
        window.open("add-dynamic");
    }

    // 执行调整点赞、浏览
    function eidtAdjustment(dynamicId){
        $("#doAdjustment").on("click", function (){
            var eidtAdjustment = 'eidt-adjustment';
            var like_count = $('#likeCount').val();
            var browse_count = $('#browseCount').val();

            if (like_count == 0) {
                alert('请输入需要新增点赞量');
                return;
            }

            if (browse_count == 0) {
                alert('请输入需要新增浏览量');
                return;
            }

            var params = {
                dynamic_id : dynamicId,
                like_count : like_count,
                browse_count : browse_count
            }

            $(".loading-area").show();
            sendAjax(eidtAdjustment, "POST", params, false, function(res){
                var result = JSON.parse(res);
                if (result.code == 0) {
                    console.log(result.msg);
                    location.reload();
                } else {
                    alert(result.msg);
                }
            });
        });
        $('#adjustment').show();
    }

    // 隐藏调整量页面
    function hideAdjustment(){
        $('#adjustment').hide();
    }

</script>